package chapter19;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/4/3 13:50
 */
public class PostOffice {
    static void handle(Mail mail) {
        for (MailHandler handler : MailHandler.values()) {
            if (handler.handle(mail)) {
                return;
            }
        }
        System.out.println(mail + " is a dead letter");
    }

    public static void main(String[] args) {
        for (Mail mail : RandomMail.generator(10)) {
            System.out.println(mail.details());
            handle(mail);
            System.out.println("------------------");
        }
    }

    enum MailHandler {
        GENERAL_DELIVERY {
            @Override
            boolean handle(Mail mail) {
                switch (mail.generalDelivery) {
                    case YES:
                        System.out.println("Using general delivery for " + mail);
                        return true;
                    default:
                        return false;
                }
            }
        },
        MACHINE_SCAN {
            @Override
            boolean handle(Mail mail) {
                switch (mail.scannability) {
                    case UNSCANNABLE:
                        return false;
                    default:
                        System.out.println("delivering " + mail + " automatically");
                        return true;
                }
            }
        },
        VISUAL_INSPECTION {
            @Override
            boolean handle(Mail mail) {
                switch (mail.readability) {
                    case ILLEGIBLE:
                        return false;
                    default:
                        System.out.println("delivering " + mail + " normally");
                        return true;
                }
            }
        },
        RETURN_TO_SENDER {
            @Override
            boolean handle(Mail mail) {
                switch (mail.returnAddress) {
                    case MISSING:
                        return false;
                    default:
                        System.out.println("returning " + mail + " to sender");
                        return true;
                }
            }
        };

        abstract boolean handle(Mail mail);
    }
}
